package benlai_miniprogram.MpCartApiServicesHost;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ocg.common.re.Assertion;
import ocg.http.httpcore.Http;
import ocg.http.httpcore.HttpFactory;
import ocg.http.httpcore.HttpModel;
import ocg.http.httpcore.HttpResponse;

/**
 * Created by chenbo on 2018/5/15.
 * $ 购物车服务
 * $ 获取购物车信息{ 商品信息、优惠金额、促销计算}
 */
public class LoadCartInfo {

    public static void loadCartInfo(){
        HttpModel model = Http.post ( "http://192.168.60.73:10102/Cart/LoadCartInfo" )
                .body ( "{\n" +
                        "    \"UnionId\": \"oSPcEj7klnq4K6wzkQutDYUaUILE\",\n" +
                        "    \"CustomerSysNo\": 0,\n" +
                        "    \"WebSiteSysNo\": 3,\n" +
                        "    \"DeliverySysNo\": 120\n" +
                        "}" ).asJson ();

        HttpResponse response = HttpFactory.factory ( model );

        JSONObject res = response.jsonBody ();

        String success = res.getString ( "Msg" );

        if (Assertion.isEquals ( success , "success" ,"/LoadCartInfo 请求成功")) {
            JSONObject value = res.getJSONObject ( "Value" );
            if (value.getIntValue ( "TotalCount" ) > 0 && value.getIntValue ( "SelectedCount" )>0) {
                JSONObject HitPromotionList = value.getJSONObject ( "HitPromotionList" );
                int promot_size = HitPromotionList.size ();

                if (promot_size>0) {
                    int PromotionDiscountAmt = value.getIntValue ( "PromotionDiscountAmt" );
                    Assertion.isNotEquals ( PromotionDiscountAmt , 0 , "命中促销后，显示促销优惠金额！" );

                    for (String key : HitPromotionList.keySet ()) {
                        JSONObject HitPromotion = HitPromotionList.getJSONArray ( key ).getJSONObject ( 0 );

//                        -- ConditionType 1-达到购买金额 2-达到购买数量
//                        -- ConditionValue 满足条件
                        String NewPromotionsCondition_sql = "SELECT SysNo,ConditionType , ConditionValue FROM NewPromotionsCondition WHERE NewPromotionsSysNo = " +key;

//                        ContentType  0-全部打折 1-减额 2-赠品 3-加价购 4-总金额
//                        ContentValue  优惠价格 加价购价格
                        String NewPromotionsContent_sql = "SELECT ContentType,ContentValue FROM NewPromotionsContent WHERE ConditionSysNo = ";

                    }
                }
            }
        }
    }
}
